//添加用户窗口
function addUserWidonw() {
    closeWindow();
    var cont = '';
    cont += "<div id='modal-wizard' class='modal'>";
    cont += "            <div class='modal-dialog'>";
    cont += "                <div class='modal-content'>";
    cont += "                    <div class='modal-header' data-target='#modal-step-contents'>";
    cont += "                        <h3>策略库设置</h3>";
    cont += "                    </div>";
    cont += "                    <div class='modal-body step-content' id='modal-step-contents' style='max-height:300px;overflow-y: auto;'>";
    cont += "                        <div class='' id='modal-step1'>";
    cont += "                            <div class='center'>";
    cont += "                                <form class='form-horizontal'>";
    cont += "                                    <div class='form-group'>";
    cont += "                                        <label for='username' class='col-sm-3 control-label no-padding-right'>用户名称：</label>";
    cont += "                                        <div class='col-sm-9'>";
    cont += "                                            <input type='text' class='col-xs-8 username' id='username' name='username' required>";
    cont += "                                        </div>";
    cont += "                                    </div>";
    cont += "                                    <div class='form-group'>";
    cont += "                                        <label for='password' class='col-sm-3 control-label no-padding-right'>登录密码：</label>";
    cont += "                                        <div class='col-sm-9'>";
    cont += "                                            <input type='password' class='col-xs-8 password length32' id='password' name='password' required>";
    cont += "                                        </div>";
    cont += "                                    </div>";
    cont += "                                    <div class='form-group'>";
    cont += "                                        <label for='email' class='col-sm-3 control-label no-padding-right'>Email地址：</label>";
    cont += "                                        <div class='col-sm-9'>";
    cont += "                                            <input type='text' class='col-xs-8 email' id='email' name='email' required>";
    cont += "                                        </div>";
    cont += "                                    </div>";
    cont += "                                    <div class='form-group'>";
    cont += "                                        <label for='describe' class='col-sm-3 control-label no-padding-right'>描述：</label>";
    cont += "                                        <div class='col-sm-9'>";
    cont += "                                            <textarea class='col-xs-8 describe' id='describe'></textarea>";
    cont += "                                        </div>";
    cont += "                                    </div>";
    cont += "                                </form>";
    cont += "                            </div>";
    cont += "                        </div>";
    cont += "                    </div>";
    cont += "                    <div class='modal-footer wizard-actions'>";
    cont += "                        <button class='btn btn-success btn-sm btn-next save' data-last='完成 '>";
    cont += "                            完成";
    cont += "                            <i class='icon-ok'></i>";
    cont += "                        </button>";
    cont += "                        <button class='btn btn-danger btn-sm pull-left' data-dismiss='modal'>";
    cont += "                            <i class='icon-remove'></i>";
    cont += "                            取消";
    cont += "                        </button>";
    cont += "                    </div>";
    cont += "                </div>";
    cont += "            </div>";
    cont += "        </div>";
    $("body").append(cont);
    $('.modal-body,.comments').slimScroll();
    $('#modal-wizard').modal("show");
}

//关闭窗口
function closeWindow() {
    $('#modal-wizard').modal("hide");
    $('#modal-wizard').remove();
}
//保存用户
function saveUser() {
    var form = $(".form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var obj = {};
    obj.username = $("#username").val();
    obj.password = $("#password").val();
    obj.email = $("#email").val();
    obj.describe = $("#describe").val();
    $.post("../action/user/create.do", {json: JSON.stringify(obj)}, function(data) {
        if (data.result == "true") {
            alert("添加成功！");
            closeWindow();
            window.location.reload();
        } else {
            alert("添加失败！用户名可能已存在。")
        }
    }, 'json')
}
//删除用户
function delUser(obj) {
    var flag = window.confirm("是否删除该用户？")
    if (flag == false) {
        return
    }
    $.post("../action/user/delete.do", {user: obj.name}, function(data) {
        if (data.result == "true") {
            alert("删除成功");
            closeWindow();
            window.location.reload();
        } else {
            alert("删除失败")
        }
    }, 'json')
}
//更新用户信息
function updataUser() {
    var form = $(".form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var obj = {};
    obj.username = $("#username").val();
    obj.password = $("#password").val();
    obj.email = $("#email").val();
    obj.describe = $("#describe").val();
    if (obj.password === pd) {
        obj.type = 0;
    } else {
        obj.type = 1;
    }
    $.post("../action/user/edit.do", {json: JSON.stringify(obj)}, function(data) {
        if (data.result == "true") {
            alert("修改成功");
            closeWindow();
            window.location.reload();
        } else {
            alert("修改失败")
        }
    }, 'json')
}
//添加用户
function addUser() {
    addUserWidonw();
    $(".save").attr("onclick", "saveUser()");
}
//编辑用户信息
var pd;
function editUser(obj) {
    addUserWidonw();
    $("#username").attr("disabled", "disabled")
    $("#username").val(obj.name);
    $("#password").val(obj.password);
    $("#email").val(obj.email);
    $("#describe").val(obj.desc);
    pd = obj.password;
    $(".save").attr("onclick", "updataUser()");
}
//查看用户列表
function searchList() {
    $.post("../action/user/list.do", {}, function(data) {
//        console.log(data);
        showTable(data);
    }, 'json')
}

//实例化表格
function showTable(souData) {
    var aaData = [];
    for (var i in souData) {
        var name = souData[i].name;
        var email = souData[i].email;
        var desc = souData[i].desc;
        var last_ip = souData[i].last_ip;
        var lats_in = souData[i].lats_in;
        var last_out = souData[i].last_out;
        var isOn = (souData[i].isOn == 1) ? "在线" : "离线";
        var oper = '';
        oper += "<a class='green' href='javascript:void(0)' title='编辑设备信息' onclick='editUser(" + JSON.stringify(souData[i]) + ")'>";
        oper += "    <i class='icon-pencil bigger-130'></i>";
        oper += "</a>";
        oper += "<a class='red' href='javascript:void(0)' title='删除设备' onclick='delUser(" + JSON.stringify(souData[i]) + ")'>";
        oper += "    <i class='icon-trash bigger-130'></i>";
        oper += "</a>";
        aaData.push([name, last_ip, lats_in, last_out, isOn, email, desc, oper]);
    }
    var oTable1 = $('#sample-table-2').dataTable({
        "aoColumns": [
            {"bSortable": true},
            {"bSortable": false},
            {"bSortable": false},
            {"bSortable": false},
            {"bSortable": false},
            {"bSortable": false},
            {"bSortable": false},
            {"bSortable": false}
        ],
        "aaData": aaData
    });
}
